package com.cskaoyan.controller;

import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.WxCouponListVo;
import com.cskaoyan.service.WxCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 优惠券
 * @Author: JianhuaZeng
 * @Date: 2023/1/2 10:49
 */
@RestController
@RequestMapping("wx/coupon")
public class WxCouponController {

    @Autowired
    WxCouponService couponService;

    /**
     * @description: 展示所有的系统优惠券
     * @param: pageInfo
     * @return: com.cskaoyan.bean.common.BaseRespVo
     * @author: JianhuaZeng
     * @date: 2023/1/2 2023/1/2
     */
    @GetMapping("list")
    public BaseRespVo list(BasePageInfo pageInfo){
        CommonData<WxCouponListVo> commonData = couponService.list(pageInfo);
        return BaseRespVo.ok(commonData,"成功");
    }

    /**
     * 查询用户对应的优惠券
     * @param status
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("mylist")
    public BaseRespVo myList(Short status,Integer page,Integer limit){
        CommonData commonData = couponService.myList(status, page, limit);
        return  BaseRespVo.ok(commonData,"成功");
    }

    /**
     * @description: 下单时查询用户的优惠券
     * @return: com.cskaoyan.bean.common.BaseRespVo
     * @author: JianhuaZeng
     * @date: 2023/1/2 2023/1/2
     */
    @GetMapping("selectlist")
    public BaseRespVo selectList(Integer cartId,Integer grouponRulesId){
        CommonData commonData = couponService.selectList(cartId, grouponRulesId);
        return BaseRespVo.ok(commonData,"成功");
    }

    /**
     * @description: 领取优惠券
     * @param: couponId
     * @return: com.cskaoyan.bean.common.BaseRespVo
     * @author: JianhuaZeng
     * @date: 2023/1/2 2023/1/2
     */
    @PostMapping("receive")
    public BaseRespVo receive(@RequestBody Map map){
        Integer couponId = (Integer) map.get("couponId");
        // code 0:优惠券已领取过 1:领取优惠券成功
        Integer code = couponService.receive(couponId);
        if (code==0){
            return BaseRespVo.errMsgAndNo(740,"优惠券已经领取过");
        }
        return BaseRespVo.ok("成功");
    }

    /**
     * @description: 根据code优惠券
     * @param: code
     * @return: com.cskaoyan.bean.common.BaseRespVo
     * @author: JianhuaZeng
     * @date: 2023/1/3 2023/1/3
     */
    @PostMapping("exchange")
    public BaseRespVo exchange(@RequestBody Map map){
        String code = (String) map.get("code");
        // status 0:优惠券不存在 1:领取成功
        Integer status = couponService.exchange(code);
        if (status == 0){
            return BaseRespVo.errMsgAndNo(742,"优惠券不正确");
        }
        return BaseRespVo.ok("成功");
    }
}
